#include "stm32f0_discovery.h"
#include "uart.h"


uint32_t msec_tick = 0;
volatile uint32_t uart_ready = 0;


/**
* @brief   		Initialize STM32F0 Discovery board - LED3, LED4, pushbutton
* @param  		None
* @retval 		None
*/
void Board_Init() {
	RCC_ClocksTypeDef RCC_Clocks;
	STM_EVAL_LEDInit(LED3);
	STM_EVAL_LEDInit(LED4);
	STM_EVAL_PBInit(BUTTON_USER, BUTTON_MODE_GPIO);
	
	// Configure SysTick for 1-msec interval 
  RCC_GetClocksFreq(&RCC_Clocks);
  SysTick_Config(RCC_Clocks.HCLK_Frequency / 1000);
}

/**
* @brief   		SysTick handler for delay function
* @param  		None
* @retval 		None
*/
void SysTick_Handler() {
	if (msec_tick > 0) {
		msec_tick--;
	}
}


/**
* @brief   		Delay function in millisecond
* @param  		None
* @retval 		None
*/
void DelayWait(uint32_t tick) {
  msec_tick = tick;
  while (msec_tick > 0);
}


/**
* @brief   		Hook function for UART RXNE Handler - toggle LED3
* @param  		None
* @retval 		None
*/

int count = 0;
char abcd[10] = "";

void UART_HookFunc_UART_IT_RXNE(uint32_t ch) {
	abcd[count] = (char) ch;
	count++;
	
	if(ch == '.')
		{
			if (strncmp(abcd,"onLED3.",count) == 0)
				STM_EVAL_LEDOn(LED3);
		
			
			else{
				if (strncmp(abcd,"offLED3.",count) == 0)
					STM_EVAL_LEDOff(LED3);
			}	
			
			if (strncmp(abcd,"onLED4.",count) == 0)
				STM_EVAL_LEDOn(LED4);
		
			
			else{
				if (strncmp(abcd,"offLED4.",count) == 0)
					STM_EVAL_LEDOff(LED4);
			}	
			
			count = 0;
		}			
}


/**
* @brief   		Hook function for UART TXE Handler - toggle LED4
* @param  		None
* @retval 		None
*/
void UART_HookFunc_UART_IT_TXE() {
	uart_ready = UART_Ready();	
}


/**
* @brief   		Main loop
* @param  		None
* @retval 		None
*/
int main() {
	
	Board_Init();
	UART_Config(9600);
	
	while(1) {
	}
}
